package com.mao.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2     //开启Swagger2
public class SwaggerConfig {

    //配置多个文档分组，只需要初始化多个Docket()
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("毛毛");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("Tom");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("Jenny");
    }

    //配置了Swagger的Docket的Bean实例
    @Bean
    public Docket docket(Environment environment){

        //判断是生产环境还是线上环境来显示Swagger
        Profiles profiles = Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //配置API文档分组
                .groupName("kechelle")
                //enable:是否启动Swagger,如果为false，则Swagger不能在浏览器访问
                .enable(flag)
                .select()
                //RequestHandlerSelectors配置扫描接口的方式
                //basePackage:指定要扫描的包
                //any():扫描全部的包
                //none():不扫描
                //withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
                //withMethodAnnotation:扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.mao.swagger.controller"))
                //paths():过滤路径
                //.paths(PathSelectors.ant("/mao/**"))
                .build();
    }

    //配置Swagger信息=apiInfo
    private ApiInfo apiInfo(){

        Contact contact = new Contact("毛毛","http://www.kechelle.top","lkx620523@163.com");

        return new ApiInfo(
                "kechelle的SwaggerAPI文档",
                "我能行，我可以",
                "v1.0",
                "http://www.kechelle.top",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());

    }

}
